package com.fei.springboot;

import java.io.IOException;
import java.net.InetAddress;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@EnableAutoConfiguration
@EnableTransactionManagement(order = 10) // 开启事务，并设置order值，默认是Integer的最大值
@SpringBootApplication(scanBasePackages = "com.fei.springboot")
public class Application extends SpringBootServletInitializer implements CommandLineRunner, EmbeddedServletContainerCustomizer
{
    @Value("${server.port}")
    Integer port;
    
    @Autowired
    ServletContext servletContext;
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(Application.class);
    }
    
    public static void main(String[] args)
        throws Exception
    {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer)
    {
        // configurableEmbeddedServletContainer.setPort(9090);
    }
    
    @Override
    public void run(String... args)
    {
        try
        {
            log.info("★★★★★★★★  now open Browser ★★★★★★★★ ");
            String ip = InetAddress.getLocalHost().getHostAddress();
            String url = "http://" + ip + ":" + port + servletContext.getContextPath();
            Runtime.getRuntime().exec("cmd /c start /min " + url + "/doc.html");
        }
        catch (IOException e)
        {
            log.error(e.getMessage(), e);
        }
    }
}
